'use strict'
/**
 * 
 * 该文件中的接口为后台管理系统使用
 */
class user{
    constructor () {

    }
    // __mid(){// 启用token-verify中间件
    //     return [
    //       {
    //         name:'token-verify',
    //         path:['list','get','post','delete']
    //       }
    //     ]
    // }
    async list (c) { // 用户列表展示
        let sqltext = `SELECT "userId","openId","userDay","userPrivilege","userInfo" FROM users`
        try {
            let r = await c.service.psql.query(sqltext,[]);
            c.send(JSON.stringify(r.rows))
            
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async get (c) { // 单个用户信息展示(不展示用户隐私信息)
        let sqltext = `SELECT "userId","openId","userDay","userPrivilege" FROM users WHERE "userId"=$1`
        try {
            let r = await c.service.psql.query(sqltext,[c.param.id]);
            if (r.rowCount != 0) { 
                c.send(JSON.stringify(r.rows))
            } else {
                c.status(500).send('Show user failed!')
            }
        } catch (err) {
            c.status(500).send("Database's operations failed!")
        }
    }
    async post (c) {  // 更新/创建新用户
        let isUpdate = c.headers.update;
        // let userId = parseInt(c.box.user.userId);
        console.log('isUpdate：',isUpdate);
        console.log("typeof isUpdate：",typeof isUpdate)
        if (isUpdate == 'true') { //更新用户
            try {
                console.log("标志1");
                let sqltext = `UPDATE users SET "openId"=$1,"userInfo"=$2,"userDay"=$3,"userPrivilege"=$4 WHERE "userId"=$5`  
                let data = JSON.parse(c.body);
                console.log("data：",data)
                let {userId, userInfo, userDay, userPrivilege, openId} = data;
                console.log("-------------------------")
                console.log(userId)
                console.log(typeof userId)
                console.log(userInfo)
                console.log(userDay)
                console.log(userPrivilege)
                console.log(openId)
                console.log("-------------------------")
                // 此处还需要根据具体提交的数据格式对userInfo进行处理，处理成数据库里对象的格式
                let r = await c.service.psql.query(sqltext,[openId,userInfo,userDay,userPrivilege,userId]) 
                console.log("标志2");
                console.log("r：",r)
                if (r.rowCount != 0) { 
                    c.send('Update user successfully!')
                } else {
                    console.log('标志3')
                    c.status(500).send('Update user failed!')
                }
            } catch (error) {
                console.log('标志4')
                c.status(500).send("Database's operations failed!")
            }
        }
        else{ // 添加用户
            // let userId = c.box.user.userId;
            try {
                console.log("标志1");
                let sqltext = `INSERT INTO users("openId","userInfo","userDay","userPrivilege")`  
                            +`VALUES($1,$2,$3,$4)`;
                let data = JSON.parse(c.body);
                console.log("data：",data)
                let r = await c.service.psql.query(sqltext,[data.openId,data.userInfo,data.userDay,data.userPrivilege]) 
                console.log("标志2");
                console.log("r：",r)
                if (r.rowCount != 0) { 
                    c.send('Add user successfully!')
                } else {
                    c.status(500).send('Add user failed!')
                }
            } catch (error) {
                c.status(500).send("Database's operations failed!")
            }
        }
        // try {
        //     let sqltext = `INSERT INTO users("openId","userInfo","userDay")`  
        //                 +`VALUES($1,$2,$3)`;
        //     let data = JSON.parse(c.body);
        //     // 此处还需要根据具体提交的数据格式对userInfo进行处理，处理成数据库里对象的格式
        //     let r = await c.service.psql.query(sqltext,[data.openId,data.userInfo,new Date().getTime()]) 
        //     if (r.rowCount != 0) { 
        //         c.send('Add user successfully!')
        //     } else {
        //         c.status(500).send('Add user failed!')
        //     }
        // } catch (error) {
        //     c.status(500).send("Database's operations failed!")
        // }
    }
    async delete (c) { // 删除用户
        try {
            let sqltext = `DELETE FROM users WHERE "userId"=$1`;
            // 此处还需要根据具体提交的数据格式对userInfo进行处理，处理成数据库里对象的格式
            let r =await c.service.psql.query(sqltext,[c.param.id]) 
            if (r.rowCount != 0) { 
                c.send('Delete user successfully!')
            } else {
                c.status(500).send('Delete user failed!')
            }
        } catch (error) {
            c.status(500).send("Database's operations failed!")
        }
    }
}
module.exports = user;